﻿#include "FriendCommand.h"

#include "../../../Memory/GameData.h"

std::set<std::u8string> FriendList{};

fast_io::vector<aliasList_struct> Friend_playerList{};
fast_io::vector<aliasList_struct> Friend_friendList{};

bool FriendCommand_onExecute(const fast_io::vector<std::u8string>& args) {
	if (args.size() == 2) {
		if (args[1] == u8"list") {
			if (FriendList.empty()) {
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"No Players in Friend List");
			} else {
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"Friend List:");
				for (auto& i : FriendList)
					C_GuiData::displayClientMessageC(Utils::TextColor::GRAY, i);
			}
			return true;
		} else
			return false;
	}

	if (args.size() < 3)
		return false;

	std::u8string concatstr{};
	for (size_t i = 2; i < args.size(); i++) {
		concatstr.append(args[i]);
		if (i != args.size() - 1) [[likely]]
			concatstr.push_back(u8' ');
	}

	if (args[1] == u8"add") {
		if (FriendList.contains(concatstr)) {
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"This player is already your friend");
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"此玩家已经是你的朋友");
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"此玩家已經是你的朋友");
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::YELLOW,
												 u8"このプレイヤーはすでにあなたの友達です");
				break;
				std::unreachable();
			}
		} else {
			FriendList.insert(concatstr);
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"The player ",
												 Utils::TextColor::RESET,
												 concatstr,
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 u8" has been added as a friend by you");
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"玩家 ",
												 Utils::TextColor::RESET,
												 concatstr,
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 u8" 已经被你添加成朋友");
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"玩家 ",
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 concatstr,
												 Utils::TextColor::RESET,
												 u8" 已經被你添加成朋友");
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"プレイヤー ",
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 concatstr,
												 Utils::TextColor::RESET,
												 u8" はあなたに友達として追加されました");
				break;
				std::unreachable();
			}
		}
	} else if (args[1] == u8"remove") {
		if (FriendList.contains(concatstr)) {
			FriendList.erase(concatstr);
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"Friend ",
												 Utils::TextColor::RESET,
												 concatstr,
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 u8" has been deleted");
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"朋友 ",
												 Utils::TextColor::RESET,
												 concatstr,
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 u8" 已经被删除");
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"朋友 ",
												 Utils::TextColor::RESET,
												 concatstr,
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 u8" 已經被刪除");
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::GREEN,
												 u8"友人 ",
												 Utils::TextColor::RESET,
												 concatstr,
												 Utils::TextColor::RESET,
												 Utils::TextColor::GREEN,
												 u8" は削除されました");
				break;
				std::unreachable();
			}
		} else {
			switch (SettingManager::languageget) {
			case 0:
			default:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"Player does not exist");
				break;
			case 1:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"玩家不存在");
				break;
			case 2:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"玩家不存在");
				break;
			case 3:
				C_GuiData::displayClientMessageC(Utils::TextColor::LIGHT_PURPLE,
												 u8"[Helium] ",
												 Utils::TextColor::RED,
												 u8"プレイヤーは存在しません");
				break;
				std::unreachable();
			}
		}
	} else
		return false;

	return true;
}

bool listPlayer_FriendCommand(fast_io::vector<std::u8string>*) {
	Friend_playerList.clear();
	GameData::forEachEntity([](C_Entity* ent) {
		if (ent != GameData::getLocalPlayer() && ent->isPlayer())
			if (auto name = ent->getNameTag()->getString(); !name.empty()) [[likely]]
				Friend_playerList.emplace_back(string_Object{std::u8string{name}, nullptr});
	});

	return true;
}

bool listFriend_FriendCommand(fast_io::vector<std::u8string>*) {
	Friend_friendList.clear();
	Friend_friendList.reserve(FriendList.size());
	for (auto& i : FriendList)
		Friend_friendList.emplace_back(stringview_Object{i, nullptr});

	return true;
}

ICommand FriendCommand{u8"friend",
					   u8".friend [add/ remove] <name>, list",
					   {u8"Add or remove friendly players", u8"添加或删除友好玩家", u8"添加或刪除友好玩家", u8"味方プレイヤーの追加または削除"},
					   {},
					   fast_io::vector<aliasList_struct>{
						   aliasList_struct{stringview_Judge{u8"add", &Friend_playerList, &listPlayer_FriendCommand}},
						   aliasList_struct{stringview_Judge{u8"remove", &Friend_friendList, &listFriend_FriendCommand}},
						   aliasList_struct{stringview_Judge{u8"list", nullptr, &listFriend_FriendCommand}},
					   },  // If empty must use fast_io::vector<aliasList_struct>{}
					   nullptr,
					   &FriendCommand_onExecute};